<template>
	<view class="page">
		<!-- 手机信息 -->
		<view class="infoBox">
			<view class="imageBox">
				<image :src="imageUrl" mode="aspectFit"></image>
			</view>
			<view class="" style="margin-left: 20rpx;">
				<view class="name">
					{{product_name}}
				</view>
				<view class="sku">
					规格：{{product_sku_id}}
				</view>
				<view class="num">
					数量：1
				</view>
			</view>
		</view>
		<!-- 客户信息 -->
		<view class="mainBox" style="height: 550rpx;">
			<view class="title">
				客户信息
			</view>
			<view class="">
				<view class="priceItem">
					<view class="Pitem" >
						<view class="">
							下单人姓名：
						</view>
						<view class="" style="display: flex;">
							<input type="text" v-model="username" placeholder="请输入下单人姓名" style="font-size: 25rpx;background-color: #f6f6f6;width: 220rpx;"/>
						</view>
					</view>
				</view>
			</view>
			<view class="">
				<view class="priceItem">
					<view class="Pitem" >
						<view class="">
							身份证号码：
						</view>
						<view class="" style="display: flex;">
							<input type="text" v-model="usernum" placeholder="请输入身份证号码" style="font-size: 25rpx;background-color: #f6f6f6;width: 220rpx;"/>
						</view>
					</view>
				</view>
			</view>
			<view class="">
				<view class="priceItem">
					<view class="Pitem" >
						<view class="">
							手机号码：
						</view>
						<view class="" style="display: flex;">
							<input type="text" v-model="phone" placeholder="请输入手机号码" style="font-size: 25rpx;background-color: #f6f6f6;width: 220rpx;"/>
						</view>
					</view>
				</view>
			</view>
			<view class="">
				<view class="priceItem">
					<view class="Pitem" @click="getAddress">
						<view class="">
							收货地址：
						</view>
						<view class="" style="display: flex;">
							<input type="text" v-model="shoeAddress" placeholder="请输入收货地址" style="font-size: 25rpx;background-color: #f6f6f6;width: 400rpx;"/>
						</view>
					</view>
				</view>
			</view>
			<view class="">
				<view class="priceItem">
					<view class="Pitem" >
						<view class="">
							邮箱地址：
						</view>
						<view class="" style="display: flex;">
							<input type="text" v-model="e_mail" placeholder="请输入邮箱地址" style="font-size: 25rpx;background-color: #f6f6f6;width: 220rpx;"/>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部  确定 -->
		<view class="botBox">
			<view class="submitBtn" @click="submitOrder">
				确定订单
			</view>
		</view>
	</view>
</template>
<script>
	import {
		pay
	} from '@/common/pay.js';
	export default {
		data() {
			return {
				// 邮箱地址
				e_mail:'',
				// 收货地址
				address:{},
				shoeAddress:'',
				// 姓名
				username:'',
				// 身份证
				usernum:'',
				phone:'',
				options:{},
				imageUrl:'',
				product_name:'',
				order_id:'',
				bill_id:'',
				product_sku_id:''
			}
		},
		methods: {
			// 获取商品详情  商品图片 标题
			getShopInfo(){
				let self = this
				self._post('product.product/detail', {
					app_id: 10001,
					product_id: self.options.product_id,
					url: '',
					visitcode: ''
				}, res => {
					self.imageUrl = res.data.detail.image[0].file_path
					self.product_name = res.data.detail.product_name
					res.data.specData.spec_attr.forEach(item => {
						if(item.group_name == '内存'){
							item.spec_items.forEach(item1 => {
								let ceshi = self.options.product_sku_id.replace('_1','')
								if(item1.item_id == ceshi) {
									self.product_sku_id = item1.spec_value
								}
							})
						}
					})
				})
			},
			// 获取地址和手机号
			getAddress(){
				my.getAddress({
				  success: (res) => {
				   if (res.resultStatus == 9000) {
					this.address.name = res.result.fullname
					this.address.phone = res.result.mobilePhone
					this.address.province = res.result.prov
					this.address.city = res.result.city
					this.address.region = res.result.area
					this.address.detail = res.result.address
					this.shoeAddress = this.address.province  + this.address.city  + this.address.region + '-'  +this.address.detail
					this.phone = this.address.phone
				   } else {
					 console.log("getAddress cancel", JSON.stringify(res));
				   }
				  },
				  fail: (res) => {
					my.alert({
					  title: "getAddress fail",
					  content: JSON.stringify(res)
					});
				  }
				});
			},
			// 提交订单
			submitOrder(){
				let self = this
				// 前置判断
				if(!self.username) {
					uni.showToast({
						title: "请填写下单人姓名！",
						duration: 1000,
						position: 200,
					})
					return
				}
				if(!self.usernum) {
					uni.showToast({
						title: "请填写下单人身份证！",
						duration: 1000,
						position: 200,
					})
					return
				}
				if(!self.phone) {
					uni.showToast({
						title: "请填写手机号码！",
						duration: 1000,
						position: 200,
					})
					return
				}
				if(!self.e_mail) {
					uni.showToast({
						title: "请填写邮箱地址！",
						duration: 1000,
						position: 200,
					})
					return
				}
				if(!self.address) {
					uni.showToast({
						title: "请填写收货地址！",
						duration: 1000,
						position: 200,
					})
					return
				}
				// 请求的参数
				let params = {
					delivery: self.delivery,
					store_id: self.store_id,
					coupon_id: self.coupon_id,
					is_use_points: self.is_use_points,
					pay_source: self.getPlatform(),
					address: self.address,
					car_lease_price:self.options.price,
					total_credit_limit_of_huabei:self.options.total_credit_limit_of_huabei,
					sesame_credit_score:self.options.sesame_credit_score,
					down_payment:self.options.down_payment,
					car_lease:self.options.car_lease,
					e_mail:self.e_mail,
					username:self.username,
					usernum:self.usernum,
					phone:self.phone,
					old_and_new:self.options.old_and_new,
					supplier: {
					        1: {
					            coupon_id: 0,
					            delivery: "10",
					            store_id: 0,
					            remark: ""
					        }
					    }
				};
				self._post(
					'order.order/buy', {
						params: JSON.stringify(
							Object.assign({}, params, {
								product_id: self.options.product_id,
								product_num: self.options.product_num,
								product_sku_id: self.options.product_sku_id,
							})
						)
					},
					function(res) {
						self.order_id = res.data.order_id[0]
						// 获取订单详情
						self.getOrderDetail()
					}
				);
			},
			// 获取订单详情
			getOrderDetail(){
				let self = this
				self._get('user.order/detail',{
					order_id:self.order_id,
					pay_source: self.getPlatform()
				},
				function(res) {
					self.bill_id = res.data.orderBill[0].bill_id
					self.payyj()
				}
				)
			},
			payyj(){
				let self = this
				self._post('/user.order/billPay', {
						bill_id: self.bill_id
					},
					function(res) {
						pay(res, self, self.paySuccess, self.payError);
					})
			}
		},
		created() {
			
		},
		mounted() {},
		onLoad(options) {
			console.log(options,'=---options');
			this.options = options
			this.getShopInfo()
		},
	}
</script>

<style lang="less">
	.page {
		background-color: #f9ffff;
	}
	.mainBox{
		background-color: #ffffff;
		box-shadow: 3rpx 3rpx 3rpx 3rpx hsl(0deg 10% 87%);
		// height: 450rpx;
		margin: 30rpx 30rpx;
		padding: 20rpx 30rpx;
	}
	
	.title {
		color: #000000;
		font-size: 27rpx;
		font-weight: 800;
	}
	.infoBox {
		height: 150rpx;
		display: flex;
		margin: 40rpx 25rpx;
	}
	.imageBox {
		width: 150rpx;
		height: 150rpx;
	}
	.imageBox image {
		width: 100%;
		height: 100%;
	}
	.botBox {
		width: 100%;
		height: 150rpx;
		position: fixed;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.submitBtn {
		width: 90%;
		border-radius: 40rpx;
		height: 80rpx;
		background-color: #3FD2CC;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #ffffff;
	}
	.priceItem {
		width: 100%;
		height: 80rpx;
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 15rpx;
	}
	.Pitem{
		background-color: #f6f6f6;
		flex: 1;
		border-radius: 10rpx;
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 30rpx;
		display: flex;
		justify-content: space-between;
		margin-top: 15rpx;
	}
</style>